package com.neuedu.shop.controller;


import com.neuedu.shop.entity.Admin;
import com.neuedu.shop.service.AdminService;
import com.neuedu.shop.util.CommonUtil;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.Y鑫
 * @since 2021-04-12
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService service;

    @RequestMapping(value = "/admins",method = RequestMethod.GET)
    public String selectAll(ModelMap map){

        /*List<Admin> admins = service.selectList(null);
        map.addAttribute("admins",admins);*/
        
        map.addAttribute("admins",service.selectList(null));
        return "admins.jsp";
    }

    @RequestMapping(value = "/insertAdmin",method = RequestMethod.POST)
    public String insertAdmin(Admin admin){
/*System.out.println(admin);*/
        service.insertAdmin(admin);
        return "redirect:admins";
    }

    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public String findById(Integer id,ModelMap map){
        map.addAttribute("admin",service.selectById(id));
        return "forward:update_admin.jsp";
    }

    @RequestMapping(value = "/getOriginPwd",method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    @ResponseBody // SpringMVC 处理 Ajax 返回的是 JSON 对象
    public String getOriginPwd(Integer id,String pwd){
/*System.out.println(id + "," + pwd);*/
        Admin admin = service.selectById(id);
        List<String> strs = new ArrayList<>();
        strs.add("原始密码验证成功!");
        strs.add("原始密码验证失败!");
        if (admin.getAdminPassword().equals(CommonUtil.getMD5(pwd))){
            return strs.get(0);
        }else {
            return strs.get(1);
        }
    }

    @RequestMapping(value = "/updateAdmin",method = RequestMethod.POST)
    public String updateAdmin(Admin admin){
/*System.out.println(admin);*/
        service.updateAdmin(admin);
        return "redirect:admins";
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Long id){
System.out.println(id);
        service.deleteById(id);
        return "redirect:admins";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Admin admin, String code, HttpSession session, HttpServletRequest request){
        String path = request.getServletContext().getContextPath();
        Admin adminDB = service.login(admin.getAdminName());
        if (adminDB != null && adminDB.getAdminPassword().equals(admin.getAdminPassword())){
            String number = (String) session.getAttribute("number");
System.out.println("从session中获取：" + number);
System.out.println("前台传入：" + admin + ",用户输入：" + code);
            // 验证码相等
            if (number.equals(code)){
                // 登录成功
                session.setAttribute("admin",admin);
                return "redirect:/index.jsp";
            }
            // 验证码不相等
            else {
                request.setAttribute("code_error","验证码不一致");
                return "forward:login.jsp";
            }
        }else {
            // 登录失败
            if (adminDB == null){
                request.setAttribute("login_error1","用户名不存在");
            }else if (!adminDB.getAdminPassword().equals(admin.getAdminPassword())){
                request.setAttribute("login_error2","用户名或密码错误");
            }
            return "forward:login.jsp";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        // 注销功能
        session.invalidate();
        return "redirect:login.jsp";
    }
}

